﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Web;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Portal.CMS
{
    public class AdvUploadAvatar : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AdvUploadAvatar";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Ảnh đại diện");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        System.Web.UI.WebControls.Button SaveButtonLargeOnly = null;
        System.Web.UI.WebControls.Label labelError = null;
        System.Web.UI.WebControls.TextBox txtAdvImageItemName = null;
        System.Web.UI.WebControls.TextBox txtDescription=null;
        System.Web.UI.WebControls.TextBox txtUrl = null;
        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl,System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AdvUploadImage));

            labelError = new System.Web.UI.WebControls.Label();
            labelError.CssClass = "ErrorUploadNewsImage";
        
            SaveButtonLargeOnly = new Button();
            SaveButtonLargeOnly.Text = ResourceBLL.GetText(WebPartId, "Upload", "Upload Avatar");
            SaveButtonLargeOnly.Click += new EventHandler(SaveButtonLargeOnly_Click);

            txtDescription = new TextBox();
            txtDescription.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;

            txtUrl = new TextBox();
            txtUrl.CssClass = "cssUploadAdvImageItem";
            
            txtAdvImageItemName = new TextBox();
            txtAdvImageItemName.CssClass = "cssUploadAdvImageItem";

            
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(Title)));
            pl.Controls.Add(labelError);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId,"FileName","Upload avatar:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));
            pl.Controls.Add(SaveButtonLargeOnly);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));

            
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));

            pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
        }

      
        void SaveButtonLargeOnly_Click(object sender, EventArgs e)
        {
            try
            {
                string AdvImageId = (string)System.Web.HttpContext.Current.Request["AdvImageId"];
                string AdvImageItemId = (string)System.Web.HttpContext.Current.Request["AdvImageItemId"];
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddAdvImageItem", AdvImageId, AdvImageCls.Owner, true);

                string WorkingPath = Environments.WorkingPath;

                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                HttpPostedFile hf = (HttpPostedFile)files[0];

                string PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\AdvImage";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\AdvImage\\Images";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\AdvImage\\Images\\" + AdvImageCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\AdvImage\\Images\\" + AdvImageCls.belongSite + "\\" + AdvImageId;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                //string PathSaveCrop = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\AdvImage\\Images\\" + AdvImageCls.belongSite + "\\" + AdvImageId + "\\crop";
                //System.IO.Directory.CreateDirectory(PathSaveCrop);
                //Environments.CreateDirectoryWithWs(PathSaveCrop, WorkingPath);

                string PathAvatar = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\AdvImage\\Images\\" + AdvImageCls.belongSite + "\\" + AdvImageId + "\\avatar";
                System.IO.Directory.CreateDirectory(PathAvatar);
                Environments.CreateDirectoryWithWs(PathAvatar, WorkingPath);


                string FileSave = PathAvatar + "\\" + hf.FileName;
                string UploadName = "";
                if (hf.FileName.Length > 0)
                {
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromFile(FileSave);
                    Environments.UploadFileWithWs(FileSave, b, WorkingPath);

                    UploadName = new System.IO.FileInfo(FileSave).Name;
                }

                ServerSideUploadAvatar(AdvImageItemId, FileSave);
                txtDescription.Text = "";
                txtAdvImageItemName.Text = "";
                txtUrl.Text = "";
                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadOk", "Upload avatar thành công!");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideUploadAvatar(string AdvImageItemId, string UploadFile)
        {
            byte[] b = Environments.GetBytesFromFile(UploadFile);
            new AdvImageBLL().UpdateAvatar(null, AdvImageItemId, UploadFile, b);
            return ResourceBLL.GetText(WebPartId, "AvatarUploadOk", "Cập nhật ảnh đại diện thành công!");
        }

    }
}